package com.yuexun.dhdoctor.util;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;


import com.yuexun.dhdoctor.R;

import org.kymjs.kjframe.ui.ViewInject;

import java.io.File;

/**
 * Created by Administrator on 2017/5/24 0024.
 */

public class VoicePlayUtil {
    int time = 0;

    //语音
    File recorderFile;
    // 多媒体播放器
    private MediaPlayer mediaPlayer;

    TextView voiceTime;
    ImageView voiceFirst;
    ImageView voiceSecend;

    public VoicePlayUtil(File recorderFile, TextView voiceTime, ImageView voiceFirst, ImageView voiceSecend){
        this.recorderFile = recorderFile;
        this.voiceTime = voiceTime;
        this.voiceFirst = voiceFirst;
        this.voiceSecend = voiceSecend;
    }

    Handler handler = new Handler() {// 创建一个handler对象 ，用于监听子线程发送的消息
        public void handleMessage(Message msg)//接收消息的方法
        {
            int str = (int) msg.obj;//类型转化
            if (str == 0) {
                voiceTime.setText("0s");
                pause();
            } else {
                voiceTime.setText(str + "s");
            }
        }
    };

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            int time = (mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition()) / 1000;
            handler.postDelayed(thread, 1000);

            Message msg = new Message();//创建消息类
            msg.obj = time;//消息类对象中存入消息
            handler.sendMessage(msg);//通过handler对象发送消息
        }
    });

    Handler viewHandler = new Handler() {// 创建一个handler对象 ，用于监听子线程发送的消息
        public void handleMessage(Message msg)//接收消息的方法
        {
            int str = (int) msg.obj;//类型转化
            if (str == 0) {
                voiceFirst.setImageResource(R.drawable.voice_wave_first_unselect);
                voiceSecend.setImageResource(R.drawable.voice_wave_secend_unselect);
            } else if (str == 1) {
                voiceFirst.setImageResource(R.drawable.voice_wave_first_select);
                voiceSecend.setImageResource(R.drawable.voice_wave_secend_unselect);
            } else {
                voiceFirst.setImageResource(R.drawable.voice_wave_first_select);
                voiceSecend.setImageResource(R.drawable.voice_wave_secend_select);
            }
        }
    };

    Thread viewThread = new Thread(new Runnable() {
        @Override
        public void run() {
            if (time >= 2) {
                time = 0;
            } else {
                time++;
            }

            viewHandler.postDelayed(viewThread, 500);
            Message msg = new Message();//创建消息类
            msg.obj = time;//消息类对象中存入消息
            viewHandler.sendMessage(msg);//通过handler对象发送消息

        }
    });

    //当音乐播放时，每次按键时都会播放或者停止音乐
    public void pause() {

        voiceFirst.setImageResource(R.drawable.voice_wave_first_unselect);
        voiceSecend.setImageResource(R.drawable.voice_wave_secend_unselect);
        mediaPlayer.pause();
        handler.removeCallbacks(thread);
        viewHandler.removeCallbacks(viewThread);

    }

    public void play() {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource(recorderFile.toString());
            mediaPlayer.prepare();
            thread.start();
            viewThread.start();
            mediaPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
            ViewInject.toast("播放失败");
        }
    }
}
